//
//  AppDbCache.m
//  helper
//
//  Created by huangyi on 12-11-21.
//  Copyright (c) 2012年 huangyi. All rights reserved.
//

#import "SQLITEObj.h"
#import "TaskQueue.h"
#import "NSString+path.h"

#import "FMDatabase.h"
#import "FMDatabaseAdditions.h"
#import "FMDatabasePool.h"
#import "FMDatabaseQueue.h"

@interface SQLITEObj ()

@property(nonatomic,strong)NSString *dbfile;
@property(nonatomic,strong)FMDatabase *sqlite;
@property(nonatomic,strong)TaskQueue *queue;

@end

@implementation SQLITEObj

+(SQLITEObj*)shared{
    static SQLITEObj* singleton=nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSString *filename=@"app.db".docPath;
        singleton=[[SQLITEObj alloc] initSQLITE:filename];
    });
    return singleton;
}
-(id)initSQLITE:(NSString*)filename{
    self=[super init];
    if (self) {
        self.dbfile=nil;
        if (filename) {
            self.dbfile=[NSString stringWithString:filename];
        }
        self.sqlite=[FMDatabase databaseWithPath:self.dbfile];
        [self reloadDB];
        self.queue=[[TaskQueue alloc] init];
    }
    if (self.sqlite==nil) {
        self=nil;
    }
    return self;
}
-(NSString*)absolutePath{
    return self.dbfile;
}
-(void)dealloc{
    [self closeDB];
}
-(id)init{
    return [self initSQLITE:nil];
}

-(BOOL)openDB{
    return [self.sqlite open];
}
-(BOOL)closeDB{
    return [self.sqlite close];
}
-(BOOL)reloadDB{
    if ([self closeDB]) {
        if ([self openDB]) {
            return YES;
        }
    }
    return NO;
}

-(BOOL)clear{
    [self closeDB];
    if (self.dbfile.isFile) {
        [self.dbfile removeFile];
    }
    [self openDB];
    return YES;
}

-(NSString*)lastError{
    if (self.sqlite.hadError) {
        return [self.sqlite lastErrorMessage];
    }else{
        return nil;
    }
}

-(NSString*)queue:(dispatch_block_t)sql{
    NSString *taskid=[self.queue addTask:sql];
    [self.queue run];
    return taskid;
}

-(NSArray*)query:(NSString*)format,...{
    NSString *sql=nil;
	va_list args;
	if (format)
	{
		va_start(args, format);
        sql=[[NSString alloc] initWithFormat:format arguments:args];
		va_end(args);
	}
    NSMutableArray *array=[NSMutableArray array];
    FMResultSet *rec=[self.sqlite executeQuery:sql];
    if (rec) {
        while ([rec next]) {
            NSDictionary *row=[rec resultDictionary];
            if (row) {
                [array addObject:row];
            }
        }
        [rec close];
        return [NSArray arrayWithArray:array];
    }else{
        NSLog(@"%@",[self.sqlite lastError].description);
        return nil;
    }
}

-(int)update:(NSString*)format,...{
    NSString *sql=nil;
	va_list args;
	if (format)
	{
		va_start(args, format);
        sql=[[NSString alloc] initWithFormat:format arguments:args];
		va_end(args);
	}
    BOOL result=[self.sqlite executeUpdate:sql];
    if (result) {
        return YES;
    }else{
        NSLog(@"%@",[self.sqlite lastError].description);
        return NO;
    }
}

@end
